/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.nyquistmeteocal.beans;

import it.nyquistmeteocal.entity.Event;
import it.nyquistmeteocal.boundary.EventManager;
import it.nyquistmeteocal.entity.Notification;
import it.nyquistmeteocal.boundary.NotificationManager;
import it.nyquistmeteocal.boundary.UserManager;
import it.nyquistmeteocal.entity.UserNotification;
import it.nyquistmeteocal.boundary.UserNotificationManager;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;


/**
 *
 * @author gatb27
 */
@Named
@ApplicationScoped
public class EditEventBean {

    private Event event;
    
    private Event selected = new Event();
    
    @EJB
    private EventManager em;
    @EJB
    private NotificationManager nm;
    @EJB
    private UserNotificationManager unm;
    @EJB
    private UserManager um;
    
    private String sqlQuery;
    private List<String> Recipients;
    private Integer i;
    private Integer count=0;
    private Notification update = new Notification();
    private UserNotification usernotification = new UserNotification();
    private String text;
    private Integer notificationid;
    private Notification tmrrainy = new Notification();
    private WeatherBean wb = new WeatherBean();
    
    ///code for invites 5 users
    private String invited1 = "email";
    private String invited2 = "email";
    private String invited3 = "email";
    
    private String eventname;
    private String creator;
    private Notification notification = new Notification();
    private Integer eventid = 0;

    public String getInvited1() {
        return invited1;
    }

    public void setInvited1(String invited1) {
        this.invited1 = invited1;
    }

    public String getInvited2() {
        return invited2;
    }

    public void setInvited2(String invited2) {
        this.invited2 = invited2;
    }

    public String getInvited3() {
        return invited3;
    }

    public void setInvited3(String invited3) {
        this.invited3 = invited3;
    }
    
    
    
    public String save(){
        em.saveEvent(event);
        return "/eventData?faces-redirect=true";
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public Event getSelected() {
        return selected;
    }

    public void setSelected(Event selected) {
        this.selected = selected;
    }
    
    public String updateEvent() throws SQLException, ParseException, IOException {
        String LoggedUser = um.getLoggedUser().getEmail();
        
        if(!LoggedUser.equals(selected.getCreator())) {
            System.out.println("Not creator of the event is trying to edit the event");
        }
        String newcity = selected.getCity();
        System.out.println("@#@#@#@#" + newcity + "@#@#@#@#");
        String neweather = wb.weatherWithCityDate(newcity, selected.getEventdate());
        System.out.println(neweather);
        selected.setWeather(neweather);
        em.updateEvent(selected);
        
        Integer eventid = this.selected.getId();
        String name = this.selected.getEventname();
        String eventdate = this.selected.getEventdate();
        Recipients = nm.getRecipients(eventid);
        count = Recipients.size();
        System.out.println("the size of the Recipients List IS: " + count);

        if(count != 0) {
            update.setEventid(eventid);
            update.setType("update");
            text = "the event called " + name + " scheduled for " + eventdate +
                    " has been updated";
            update.setText(text);
            nm.saveNotification(update);
            
            notificationid = update.getId();
            usernotification.setNotificationid(notificationid);
            
            for(i=0; i<count; i++) {
                String currentRecipient = Recipients.get(i);
                usernotification.setUsermail(currentRecipient);
                unm.saveUserNotification(usernotification);
            }
            
        }
        
        if (!invited1.equals("email") || !invited2.equals("email") || !invited2.equals("email")) {
            /*  RELATED TO INVITE NOTIFICATION*/
            /*get the event*/
            eventname = this.selected.getEventname();
            System.out.println("the event name of the current event is:   " + eventname);
            /*define the text for the notification*/
            String eventdate2 = this.selected.getEventdate();
            creator=selected.getCreator();
            text = "You've been invited to " + eventname + " from " + this.creator + " on " + eventdate2;
            System.out.println("text of the notification is:   " + text);
            /*set the text for the notification*/
            this.notification.setText(this.text);
            System.out.println("text of notification after get:  " + this.notification.getText());
            /*set the type for the notification, here invite for default*/
            this.notification.setType("invite");
            System.out.println("type of notification after get:   " + this.notification.getType());
            /*set the eventid for the notification*/
            Integer eventid2 = selected.getId();
            this.notification.setEventid(eventid2);

            /*save information in db notification*/
            nm.saveNotification(notification);

            /*get id notification just created*/
            notificationid = this.notification.getId();
            System.out.println("not id is " + notificationid);
            System.out.println("first invited email " + this.invited1);

            /*set the not id in usnoti*/
            this.usernotification.setNotificationid(this.notificationid);
            System.out.println("not id is" + this.usernotification.getNotificationid());

            /*save information in db usernotification*/
            ///unm.saveUserNotification(usernotification);
            if (!invited1.equals("email")) {
                this.usernotification.setUsermail(invited1);
                unm.saveUserNotification(usernotification);
            }

            if (!invited2.equals("email")) {
                this.usernotification.setUsermail(invited2);
                unm.saveUserNotification(usernotification);
            }

            if (!invited3.equals("email")) {
                this.usernotification.setUsermail(invited3);
                unm.saveUserNotification(usernotification);
            }

        }
        
        /* List<Event> rainyEvents = em.getBadEvents();
        List<Event> tmrRainyEvents = em.getTomorrowRainyEvents();
        Integer count = rainyEvents.size();
        System.out.println("count before iterator IS:" + count + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        count = tmrRainyEvents.size();
        System.out.println("count before iterator IS:" + count + "###############################");
        int i;
        List<String> Recipients;
        
        if(count != 0){
            System.out.println("-------------I'M IN THE FIRST IF-------------");
            for(i=0; i<count; i++) {
                System.out.println("-----I'M IN THE FIRST FOR NUMBER" + i + "--------");
                Event currentEvent = tmrRainyEvents.get(i);
                Recipients = nm.getRecipients2(currentEvent.getId());
                Integer countt = Recipients.size();
                System.out.println("count of recipient IS:" + countt + "***********************");
                
                if(countt != 0) {
                    System.out.println("-------------I'M IN THE SECOND IF-------------");
                    text="The outdoor event " + currentEvent.getEventname() + 
                        " scheduled for tomorrow will have RAINY weather!";
                    tmrrainy.setText(text);
                    tmrrainy.setEventid(currentEvent.getId());
                    tmrrainy.setType("tomorrowRainy");
                    nm.saveNotification(tmrrainy);
                    
                    notificationid = tmrrainy.getId();
                    System.out.println(tmrrainy.getId());
                    
                    usernotification.setNotificationid(notificationid);
                    int j;
                    for(j=0; j<countt; j++) {
                        System.out.println("-----I'M IN THE SECONDO FOR NUMBER" + i + "--------");
                        String currentRecipient = Recipients.get(j);
                        usernotification.setUsermail(currentRecipient);
                        System.out.println(usernotification.getUsermail());
                        System.out.println(usernotification.getNotificationid());
                        unm.saveUserNotification(usernotification);
                    }
                }
            }
        }*/
        
        
        
        
        
        
        
        
        
        return "displayEvent2";
    }
    
}
